/*
 * Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
 */

package team.mai.antexpress.tests;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import team.mai.antexpress.dao.DepartmentDAO;
import team.mai.antexpress.entities.DepartmentEntity;

import java.util.ArrayList;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"applicationContext.xml"})
public class DeptDAOTest {
	@Autowired
	DepartmentDAO dao;

	@Test
	public void test() {
		ArrayList<DepartmentEntity> list = new ArrayList<>();
		list.add(new DepartmentEntity());
		list.get(0).setName("Department 1");
		list.get(0).setDescription("Department 1");
		list.add(new DepartmentEntity());
		list.get(1).setName("Department 2");
		list.get(1).setDescription("Department 2");
		list.add(new DepartmentEntity());
		list.get(2).setName("Department 3");
		list.get(2).setDescription("Department 3");
		list.add(new DepartmentEntity());
		list.get(3).setName("Department 4");
		list.get(3).setDescription("Department 4");

		ArrayList<Integer> ids = new ArrayList<>();

		try {
			for (DepartmentEntity dept : list) {
				int id = dao.insertDepartment(dept);
				ids.add(id);
				if(id<1)
					throw new Exception("ID is less than 1");
			}

			List<DepartmentEntity> allDepartments = dao.getAllDepartments();
			Assert.assertTrue(allDepartments.size() >= 4);
//		Assert.assertEquals("Department 1",allDepartments.get(ids.get(0)).getName());

			DepartmentEntity department1 = dao.getDepartment(ids.get(0));
			System.out.println("Id:0 = " + ids.get(0));

			Assert.assertEquals("Department 1", department1.getName());

			DepartmentEntity department1_ = dao.getDepartment("Department 1");

			Assert.assertEquals((Integer) ids.get(0), (Integer) department1_.getId());

			list.get(0).setName("Department 1 new");
			dao.updateDepartment(list.get(0));
			Assert.assertEquals("Department 1 new", dao.getDepartment(ids.get(0)).getName());

		} catch (Exception e) {
			e.printStackTrace();
			Assert.fail("Exception fail");
		} finally {
			dao.deleteDepartments(ids);
			DepartmentEntity department_none = dao.getDepartment(ids.get(0));
			Assert.assertEquals(null, department_none);
		}
	}
}
